#include "userdef.h"
#include "fw_hal.h"
#include <string.h>
#include <stdio.h>

void set_sum2(bmp_map_t *map,uint8_t invert,uint16_t x,uint16_t y,uint16_t val);
void lcd_flush(void);
void lcd_fill(uint8_t dat);
void motor_moveral(uint8_t dir,uint32_t sec,uint32_t accel,uint32_t speed);
void set_map_val(bmp_map_t *map,uint8_t invert,uint16_t x,uint16_t y,uint8_t val);

typedef enum {_K_NULL=0,_K_CW= 1,_K_CCW= 2,_K_EN=3,_K_LEN=4} enum_key_t;

typedef struct str_menu_t
{
	uint8_t pos;
	void (*init)(void);
	void (*mess)(enum_key_t,uint32_t,struct str_menu_t*) reentrant;
	uint8_t parent;
	uint8_t child;
	uint8_t cw;
	uint8_t ccw;
}menu_item_t;
menu_item_t *now_menu,*last_menu,*next_menu;
#define MAX_MENU_ITEM  4
extern const menu_item_t xdata menu_item[MAX_MENU_ITEM];
	
#define debugline do{UART1_TxString("line:");debug_u32(__LINE__);UART1_TxString("\r\n");}while(0)

//11*18
uint8_t code num_bmp[]=
{
0x00,0x00,0xF8,0x00,0xF8,0x00,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0xF8,0x00,0xF8,0x00,0x00,0x00,
0x00,0x00,0x60,0x00,0x78,0x00,0x7C,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0xFE,0x03,0xFE,0x03,0x00,0x00,
0x00,0x00,0xF8,0x00,0xFC,0x01,0x06,0x03,0x06,0x03,0x00,0x03,0x00,0x03,0x00,0x03,0x80,0x01,0xC0,0x01,0xE0,0x00,0x60,0x00,0x38,0x00,0x1C,0x00,0x06,0x00,0xFE,0x03,0xFE,0x03,0x00,0x00,
0x00,0x00,0xF8,0x00,0xF8,0x00,0x06,0x03,0x06,0x03,0x00,0x03,0x00,0x03,0x00,0x03,0xE0,0x00,0xE0,0x00,0x00,0x03,0x00,0x03,0x00,0x03,0x06,0x03,0x06,0x03,0xF8,0x00,0xF8,0x00,0x00,0x00,	
0x00,0x00,0xC0,0x00,0xC0,0x00,0xE0,0x00,0xE0,0x00,0xF0,0x00,0xD0,0x00,0xD8,0x00,0xD8,0x00,0xCC,0x00,0xC4,0x00,0xC6,0x00,0xFE,0x03,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0x00,0x00,
0x00,0x00,0xFE,0x01,0xFE,0x01,0x06,0x00,0x06,0x00,0x06,0x00,0x06,0x00,0xFE,0x00,0xFC,0x01,0x80,0x03,0x00,0x03,0x00,0x03,0x00,0x03,0x00,0x03,0x80,0x03,0xFE,0x01,0xFE,0x00,0x00,0x00,
0x00,0x00,0xF0,0x03,0xF8,0x03,0x1C,0x00,0x1E,0x00,0x06,0x00,0x06,0x00,0xFE,0x00,0xFE,0x00,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0xF8,0x00,0xF8,0x00,0x00,0x00,	
0x00,0x00,0xFE,0x03,0xFE,0x03,0x00,0x03,0x00,0x03,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0x30,0x00,0x30,0x00,0x30,0x00,0x30,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x00,0x00,	
0x00,0x00,0xF8,0x00,0xF8,0x00,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0xFE,0x03,0xF8,0x00,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0xF8,0x00,0xF8,0x00,0x00,0x00,
0x00,0x00,0xF8,0x00,0xF8,0x00,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0x06,0x03,0xF8,0x03,0xF8,0x03,0x00,0x03,0x00,0x03,0x80,0x01,0x80,0x01,0xF8,0x00,0xF8,0x00,0x00,0x00,	
};

bmp_map_t xdata sum_map=
{
	11,18,
	num_bmp
};

//36*36
uint8_t code wash_bmp[]=
{
0x00,0x00,0xFE,0x03,0x00,0x00,0x00,0x02,0x02,0x00,0x00,0x00,0x02,0x02,0x00,0x00,0x00,0x02,0x02,0x00,0x00,0x00,0x02,0x02,0x00,0x00,0x00,0x02,0x02,0x00,0x00,0x00,
0x02,0x02,0x00,0x00,0x00,0x02,0x02,0x00,0x00,0x00,0x02,0x02,0x00,0xE0,0xFF,0x03,0xFE,0x0F,0x20,0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,
0x08,0x20,0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x08,0xE0,0x07,0x00,0xC0,0x0F,0x00,0x04,0x00,0x40,0x00,0xE0,0x07,0x00,0xC0,0x0F,0x20,0x00,0x00,0x00,0x08,0x20,
0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x08,0x20,0x00,0x00,
0x00,0x08,0x20,0x00,0x70,0x00,0x08,0x30,0x00,0x50,0x00,0x08,0x10,0x00,0x50,0x00,0x08,0x18,0x00,0x50,0x00,0x08,0x0C,0x00,0x50,0x00,0x08,0x04,0x00,0x50,0x00,0x08,
0x07,0x00,0x50,0x00,0x0C,0x01,0x00,0x50,0x00,0x04,0x01,0x00,0x50,0x00,0x07,0xFF,0xFF,0xDF,0xFF,0x01,
	
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x01,0x00,0x00,0x00,0xFC,0x01,0x00,0x00,0x00,0xFC,0x01,0x00,0x00,0x00,0xFC,0x01,0x00,0x00,0x00,0xFC,0x01,0x00,0x00,0x00,
0xFC,0x01,0x00,0x00,0x00,0xFC,0x01,0x00,0x00,0x00,0xFC,0x01,0x00,0x00,0x00,0xFC,0x01,0x00,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,0xFF,
0x07,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,0xFF,0x07,0x00,0xF8,0xFF,0x3F,0x00,0x00,0xF8,0xFF,0x3F,0x00,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,
0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,0xFF,0x07,0xC0,0xFF,0xFF,
0xFF,0x07,0xC0,0xFF,0x8F,0xFF,0x07,0xC0,0xFF,0x8F,0xFF,0x07,0xE0,0xFF,0x8F,0xFF,0x07,0xE0,0xFF,0x8F,0xFF,0x07,0xF0,0xFF,0x8F,0xFF,0x07,0xF8,0xFF,0x8F,0xFF,0x07,
0xF8,0xFF,0x8F,0xFF,0x03,0xFE,0xFF,0x8F,0xFF,0x03,0xFE,0xFF,0x8F,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
};

bmp_map_t xdata wash_map=
{
	36,36,
	wash_bmp
};

//42*42
uint8_t code cure_bmp[]=
{
0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0x00,0x84,0x00,0x00,0x00,0x00,0x00,0x86,0x01,0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x00,0x00,0xBC,0x03,0xF7,0x00,0x00,0x00,0xE4,
0x00,0x9C,0x00,0x00,0x00,0x04,0x00,0x80,0x00,0x00,0x00,0x06,0x00,0x80,0x01,0x00,0x00,0x02,0x00,0x00,0x01,0x00,0xFE,0xF3,0xFF,0x3F,0xFF,0x01,0x06,0x18,0x00,0x60,
0x80,0x01,0x0C,0x08,0x00,0x40,0xC0,0x00,0x18,0x04,0x00,0x80,0x60,0x00,0x10,0x02,0x00,0x00,0x21,0x00,0x10,0x02,0x00,0x00,0x21,0x00,0x10,0x7B,0xCF,0x71,0x23,0x00,
0x18,0x49,0x49,0x51,0x62,0x00,0x0C,0x49,0x49,0x51,0xC2,0x00,0x07,0x49,0x49,0x51,0x82,0x03,0x01,0x49,0x49,0x51,0x02,0x02,0x01,0x49,0x49,0x4A,0x02,0x02,0x07,0x49,
0x89,0x24,0x82,0x03,0x0C,0xC9,0x89,0x24,0xC2,0x00,0x18,0x09,0x08,0x11,0x62,0x00,0x10,0x1B,0x0C,0x11,0x23,0x00,0x10,0x36,0x06,0x8A,0x21,0x00,0x10,0xE4,0x03,0x84,
0x20,0x00,0x18,0x04,0x00,0x80,0x60,0x00,0x08,0x04,0x78,0x80,0x40,0x00,0x18,0xF4,0x48,0xBC,0x60,0x00,0x30,0x94,0x48,0xA4,0x30,0x00,0xC0,0x95,0x48,0xA4,0x0E,0x00,
0x00,0x93,0x48,0x24,0x03,0x00,0x00,0x90,0x48,0x24,0x00,0x00,0x00,0x98,0x49,0x66,0x00,0x00,0x00,0x08,0x49,0x42,0x00,0x00,0x00,0x90,0xCC,0x24,0x00,0x00,0x00,0x60,
0x02,0x19,0x00,0x00,0x00,0x00,0x84,0x00,0x00,0x00,0x00,0x00,0x48,0x00,0x00,0x00,0x00,0x00,0x48,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x30,
0xFE,0x31,0x00,0x00,0x00,0xF8,0xFF,0x7F,0x00,0x00,0x00,0xF8,0xFF,0x7F,0x00,0x00,0x00,0xFC,0xFF,0xFF,0x00,0x00,0x00,0x7C,0x00,0xF8,0x00,0x00,0x00,0x1F,0x00,0xE0,
0x03,0x00,0xF8,0x0F,0x00,0xC0,0x7F,0x00,0xF0,0x07,0x00,0x80,0x3F,0x00,0xE0,0x03,0x00,0x00,0x1F,0x00,0xE0,0x03,0x00,0x00,0x1F,0x00,0xE0,0x01,0x00,0x00,0x1E,0x00,
0xE0,0x30,0x76,0x70,0x1C,0x00,0xE0,0x30,0x66,0x30,0x1C,0x00,0xF0,0x30,0x66,0x30,0x3C,0x00,0xF8,0x30,0x46,0x10,0x7C,0x00,0xFE,0x30,0xC6,0x18,0xFC,0x01,0xFE,0x30,
0x86,0x0D,0xFC,0x01,0xF8,0x30,0x86,0x0D,0x7C,0x00,0xF0,0xF0,0x07,0x07,0x3C,0x00,0xE0,0xE0,0x03,0x07,0x1C,0x00,0xE0,0xC0,0x01,0x02,0x1C,0x00,0xE0,0x01,0x00,0x00,
0x1E,0x00,0xE0,0x03,0x00,0x00,0x1F,0x00,0xE0,0x63,0x30,0x18,0x1F,0x00,0xF0,0x63,0x30,0x18,0x3F,0x00,0xE0,0x63,0x30,0x18,0x1F,0x00,0xE0,0x63,0x30,0x18,0x0F,0x00,
0x80,0x63,0x30,0x18,0x07,0x00,0x00,0x62,0x30,0x18,0x01,0x00,0x00,0x60,0x30,0x18,0x00,0x00,0x00,0xF8,0x31,0x7E,0x00,0x00,0x00,0xF0,0x30,0x3C,0x00,0x00,0x00,0x60,
0xFC,0x18,0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

bmp_map_t xdata cure_map=
{
	42,42,
	cure_bmp
};

//22*22
uint8_t code time_bmp[]=
{
0x00,0x1E,0x00,0xC0,0xFF,0x00,0xE0,0xE1,0x01,0x30,0x0C,0x03,0x18,0x0C,0x06,0x0C,0x00,0x0C,0x06,0x0C,0x18,0x06,0x0C,0x18,0x06,0x0C,0x18,0x03,0x0C,0x30,0x1B,0x0C,
0x36,0x1B,0x0C,0x36,0x03,0x18,0x30,0x06,0x30,0x18,0x06,0x60,0x18,0x06,0xC0,0x18,0x0C,0x00,0x0C,0x18,0x0C,0x06,0x30,0x0C,0x03,0xE0,0xE1,0x01,0xC0,0xFF,0x00,0x00,
0x1E,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x00,0xC0,0xF3,0x00,0xE0,0xF3,0x01,0xF0,0xFF,0x03,0xF8,0xF3,0x07,0xF8,0xF3,0x07,0xF8,0xF3,0x07,0xFC,0xF3,0x0F,0xE4,0xF3,
0x09,0xE4,0xF3,0x09,0xFC,0xE7,0x0F,0xF8,0xCF,0x07,0xF8,0x9F,0x07,0xF8,0x3F,0x07,0xF0,0xFF,0x03,0xE0,0xF3,0x01,0xC0,0xF3,0x00,0x00,0x1E,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,
};

bmp_map_t xdata time_map=
{
	22,22,
	time_bmp
};

//4*18
uint8_t code maohao_bmp[]=
{
0x00,0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,0x00,
};

bmp_map_t xdata maohao_map=
{
	4,18,
	maohao_bmp
};


void wash_icon(uint8_t invert,uint8_t v)
{
	set_map_val(&wash_map,invert,15,0,v);
}

void cure_icon(uint8_t invert,uint8_t v)
{
	set_map_val(&cure_map,invert,74,0,v);
}

void time_icon(uint8_t invert,uint8_t v)
{
	set_map_val(&time_map,invert,20,40,v);
}

void count_down(uint8_t invert,uint32_t t)
{
#define X_CD   50
#define Y_CD   44

	char xdata min_str[4];
	char xdata sec_str[4];
	int min =t/60;
	int sec =t%60;
	sprintf(min_str,"%02d",min);
	sprintf(sec_str,"%02d",sec);

	set_map_val(&sum_map,invert,X_CD,Y_CD,min_str[0]-'0');
	set_map_val(&sum_map,invert,X_CD+sum_map.x_size,Y_CD,min_str[1]-'0');
	
	set_map_val(&sum_map,invert,maohao_map.x_size+X_CD+sum_map.x_size*2,Y_CD,sec_str[0]-'0');
	set_map_val(&sum_map,invert,maohao_map.x_size+X_CD+sum_map.x_size*3,Y_CD,sec_str[1]-'0');
	
  set_map_val(&maohao_map,invert,X_CD+sum_map.x_size*2,Y_CD,0);
}

void test_bmp(void)
{
	wash_icon(0,1);
	cure_icon(0,0);
	time_icon(0,0);
	count_down(1,200);
}

void func_0_1_init(void)
{
	lcd_fill(0x00);
	wash_icon(0,1);
	cure_icon(0,0);
	time_icon(0,0);
	count_down(0,wash_time*60);
	lcd_flush();
	menu_state =_M_OTHER;	
}

void func_0_2_init(void)
{
	lcd_fill(0x00);
	wash_icon(0,0);
	cure_icon(0,1);
	time_icon(0,0);
	count_down(0,cure_time*60);
	lcd_flush();
	menu_state =_M_OTHER;	
}

void func_1_init(void)
{
	count_down(1,wash_time*60);
	lcd_flush();
}

void func_2_init(void)
{
	count_down(1,cure_time*60);
	lcd_flush();
}

void func_0_mess(enum_key_t key,uint32_t par,menu_item_t *p) reentrant
{
	 par =0;
   switch(key)
	 {
		 case _K_CW:
			 next_menu =&menu_item[p->cw];
		   break;
     case _K_CCW:	
			 next_menu =&menu_item[p->ccw];
		   break;
		 case _K_EN:
		 case _K_LEN:
			 next_menu =&menu_item[p->child];
			 break;				 
	 }
}

void task_mac_cure(uint8_t id,int32_t ntick) reentrant
{
	crBegin(id,ntick);
	menu_state =_M_TD;
	motor_control.e_stop =0;
	time_icon(0,1);
	motor_moveral(DIR_CW,down_time,2500,106);
	for(;;)
	{
		crWait(id,1000);			
    count_down(0,down_time);
		if(down_time==0||motor_control.e_stop ==1){motor_control.e_stop=2;break;}
		if(COVER_R){BUZZER=!BUZZER;UV_LED =0;continue;}
		UV_LED =1;
		BUZZER =0;
		down_time--;
		lcd_flush();
	}
	UV_LED =0;
  while(motor_control.step_state!=STA_IDLE){crReturn(id);}	
	next_menu =&menu_item[now_menu->parent];
	
	BUZZER =1;
	crWait(id,1000);
	BUZZER =0;	
  crDel(id);	
	crFinish(id)
}

void task_mac_wash(uint8_t id,int32_t ntick) reentrant
{
	static uint8_t _dir=0;
	crBegin(id,ntick);
	menu_state =_M_TD;
	motor_control.e_stop =0;
	time_icon(0,1);
	for(;;)
	{
    count_down(0,down_time);	
		if(down_time==0||motor_control.e_stop ==1){motor_control.e_stop=2;break;}
		down_time--;
		lcd_flush();
		
		if(motor_control.step_state==STA_IDLE){
			motor_moveral(_dir,60,1000,200*16*4.5);
			_dir=!_dir;
		}
		crWait(id,1000);
	}
  while(motor_control.step_state!=STA_IDLE){crReturn(id);}	
	next_menu =&menu_item[now_menu->parent];
	BUZZER =1;
	crWait(id,1000);
	BUZZER =0;	
  crDel(id);
	crFinish(id)
}

void func_1_mess(enum_key_t key,uint32_t par,menu_item_t *p) reentrant
{ 
	 par =0;
   switch(key)
	 {
		 case _K_CW:
			 if(menu_state !=_M_OTHER)break;
		   wash_time=++wash_time>99?1:wash_time;
		   count_down(1,wash_time*60);
			 lcd_flush();
		   break;
     case _K_CCW:	
			 if(menu_state !=_M_OTHER)break;
		   wash_time=--wash_time==0?99:wash_time;
		   count_down(1,wash_time*60);
			 lcd_flush();
		   break;
		 case _K_EN:
			 if(menu_state !=_M_OTHER){	
				 if(motor_control.step_state!=STA_IDLE){
					  wash_icon(0,0);
						cure_icon(0,0);
						lcd_flush();
            motor_control.e_stop =1;					 
				 }
				 break;
			 }

			 down_time =wash_time*60;
		   task_get(task_mac_wash);

			 break;
		 case _K_LEN:
			 if(menu_state ==_M_OTHER)
			 next_menu =&menu_item[p->parent];
			 break;				 
	 }
}

void func_2_mess(enum_key_t key,uint32_t par,menu_item_t *p) reentrant
{
//	 if(motor_control.e_stop==0 && menu_state!=_M_OTHER && motor_control.step_state!=STA_IDLE && COVER_R){
//		 key =_K_EN;
//	 }
	 
	 par =0;
   switch(key)
	 {
		 case _K_CW:
			if(menu_state !=_M_OTHER)break;
		  cure_time=++cure_time>99?1:cure_time;
		  count_down(1,cure_time*60);
			lcd_flush();
		   break;
     case _K_CCW:
			if(menu_state !=_M_OTHER)break;
		  cure_time=--cure_time==0?99:cure_time;
		  count_down(1,cure_time*60);
			lcd_flush();
		   break;
		 case _K_EN:
			 if(menu_state !=_M_OTHER){
				 if(motor_control.step_state!=STA_IDLE){
					  wash_icon(0,0);
						cure_icon(0,0);
						lcd_flush();
					  motor_control.e_stop =1;			 
				 }
				 break;
			 }
       
			 if(COVER_R){
				 BUZZER =1;
				 delay_nms(500);
				 BUZZER =0;	
				 delay_nms(500);
				 BUZZER =1;
				 delay_nms(500);
				 BUZZER =0;				 
				 break;
			 }
			 
			 down_time =cure_time*60;
		   task_get(task_mac_cure);

			 break;
		 case _K_LEN:
			 if(menu_state ==_M_OTHER)
			 next_menu =&menu_item[p->parent];
			 break;	 
	 }
}

/*
  parent child cw ccw
*/

const menu_item_t xdata menu_item[MAX_MENU_ITEM]={
	{0,func_0_1_init,func_0_mess,0,2,1,1},
	{1,func_0_2_init,func_0_mess,1,3,0,0},
		
	{2,func_1_init,func_1_mess,0,2,2,2},
	{3,func_2_init,func_2_mess,1,3,3,3},
};

uint32_t k_en_tick =0;
uint8_t  key_en_1=1,key_en_2=1;
int16_t  abs_diff =0;
int8_t   last_encoder_diff =0;
#define  EPPS 5
uint32_t spin_tick =0;

enum_key_t get_keyid(void)
{
	//----------------------en------------------------
	key_en_1 =ENCODER_EN;
	if(key_en_2 && !key_en_1){
		key_en_2 =key_en_1;
		k_en_tick =sys_tick;
	}else if(!key_en_2 && key_en_1){
		key_en_2 =key_en_1;
		if((sys_tick-k_en_tick)>500UL) return _K_LEN;
		else return _K_EN;
	}
  key_en_2 =key_en_1;
	//-----------------------spin--------------------------
	if((sys_tick-spin_tick)>100)
	{
		spin_tick =sys_tick;
		abs_diff =encoder_diff>=0?encoder_diff:-encoder_diff;
		
		if(encoder_diff ==last_encoder_diff && abs_diff<=EPPS/2)
			encoder_diff =0;
		else if(WITHIN(abs_diff,EPPS/2+1,EPPS-1)){
			abs_diff =EPPS;
			encoder_diff = (encoder_diff < 0 ? -1 : 1) * abs_diff;
		}
	  last_encoder_diff =encoder_diff;

		if(abs_diff>=EPPS){
			if(encoder_diff>0){
				encoder_diff =0;
				return _K_CW;
			}else{
				encoder_diff =0;
				return _K_CCW;
			}
		}
  }
	return _K_NULL;
}

void task_menu(uint8_t id,int32_t ntick) reentrant
{
	enum_key_t keyid;
	crBegin(id, ntick);
	COVER_INIT();
	CHA_FAN_INIT();
	DRY_FAN_INIT();
	LCD_BL_INIT();
	UV_LED_INIT();
	BUZZER_INIT();
	CHA_FAN =1;
	DRY_FAN =1;
	LCD_BL  =1;
	UV_LED  =0;
	BUZZER  =1;
	crWait(id,1000);
	BUZZER =0;
	last_menu =&menu_item[2];
	next_menu =&menu_item[0];
	k_en_tick =sys_tick;
	for(;;)
	{
		crReturn(id);
		keyid =get_keyid();
		now_menu =next_menu;
		if(now_menu->init!=last_menu->init){
			(now_menu->init)();
		}
		(now_menu->mess)(keyid,0UL,now_menu);
		last_menu =now_menu;
	}
	crEnd()
}




